<?php
declare (strict_types = 1);

namespace app\wechat\controller;

use app\BaseController;
use app\Request;
use app\wechat\model\MiniCodeModel;
use app\wechat\model\MiniSendMessageRecordModel;
use app\wechat\model\MiniSubscribeMessageModel;
use app\wechat\model\MiniUserModel;
use app\wechat\service\MiniService;
use app\wechat\service\MiniUserService;
use app\wechat\service\SubscribeMessageService;
use think\facade\View;

class Mini extends BaseController
{
    /**
     * 发送订阅消息
     *
     * @Author tanruizheng
     * @DateTime 2021-08-24
     * @return void
     */
    public function subscribeMessageSend(){
        try {
            $miniService = new SubscribeMessageService('wx294975311d4ef718');
            $templateId = 'TahWMGiV6PUvC2I9V9Iubnv-wcnL5tTrMzpZ0m2rzMY';
            $toUser = 'oqqPl5UsxPOSzVXZF8jvsjlyrOtA';
            $data = [
                'thing1' => [
                    'value' => 'xxx比赛'
                ],
                'date2' => [
                    'value' => '2019-10-12'
                ],
                'thing3' => [
                    'value' => '中奖'
                ],
                'thing4' => [
                    'value' => '请到xxx领取奖励'
                ],
            ];
            $res = $miniService->subscribeMessageSend($templateId, $toUser,$data);
            return json($res);
        } catch (\Exception $e) {
            return self::createFail($e->getMessage());
        }
    }

    /**
     * 获取当前帐号下的个人模板列表
     *
     * @Author tanruizheng
     * @DateTime 2021-08-24
     * @return void
     */
    public function getTemplates(){
        try {
            $SubscribeMessageService = new SubscribeMessageService('wx294975311d4ef718');
            $res = $SubscribeMessageService->getTemplates();
            return json($res);
        } catch (\Exception $e) {
            return self::createFail($e->getMessage());
        }
    }

    /**
     * 小程序授权用户
     *
     * @Author tanruizheng
     * @DateTime 2021-07-09
     * @return void
     */
    public function miniUser(Request $request){
        if($request->param('_action') == 'getUserList'){
            $params = $request->param();
            $page = $params['page'] ?: 1;//页数
            $limit = $params['limit'] ?: 15;//条数
            $adminList = MiniUserService::getUserList($params,$page,$limit);
            return json($adminList);
        }else if($request->param('_action') == 'userDel'){
            $id = $request->param('id');
            $miniUser = MiniUserModel::where('id',$id)->findOrEmpty();
            if($miniUser->isEmpty()){
                return self::createFail('用户信息不存在');
            }
            $res = $miniUser->delete();
            if($res !== false){
                return self::createSuccess('删除成功');
            }
            return self::createFail('删除失败');
        }
        return View::fetch('miniUser',[
           
        ]);
    }

    /**
     * 小程序码
     *
     * @Author tanruizheng
     * @DateTime 2021-08-25
     * @return void
     */
    public function miniCode(Request $request){
        if($request->param('_action') == 'getMiniCodeList'){
            $page = $request->param('page/d',1);
            $limit = $request->param('limit/d',15);
            $where = [];
            //列表
            $list = MiniCodeModel::where($where)->page($page,$limit)->select()->toArray();
            foreach ($list as &$item) {
               $item['type'] = $item['type'] == 'limit' ? '限制' : '不限制';
            }
            //总数
            $count = MiniCodeModel::where($where)->count();
            return json(self::createList($list,$count));
        }else if($request->param('_action') == 'miniCodeDel'){
            $id = $request->param('id');
            $MiniCodeModel = MiniCodeModel::where('id',$id)->findOrEmpty();
            if($MiniCodeModel->isEmpty()){
                return self::createFail('信息不存在');
            }
            $res = $MiniCodeModel->delete();
            if($res !== false){
                return self::createSuccess('删除成功');
            }
            return self::createFail('删除失败');
        }else if($request->param('_action') == 'miniCodeCreate'){
            return View::fetch('miniCodeCreate');
        }else if($request->param('_action') == 'miniCodeCreatePost'){
            $miniService = new MiniService('wx294975311d4ef718');
            $res = $miniService->getLimitCode('/page/index/index');
            return json($res);
        }
        return View::fetch('miniCode',[
           
        ]);
    }

    /**
     * 订阅消息
     *
     * @Author tanruizheng
     * @DateTime 2021-08-25
     * @return void
     */
    public function subscribeMessage(Request $request){
        if($request->param('_action') == 'getList'){
            $page = $request->param('page/d',1);
            $limit = $request->param('limit/d',15);
            $where = [];
            //列表
            $list = MiniSubscribeMessageModel::where($where)->page($page,$limit)->select()->toArray();
            foreach ($list as &$item) {
              
            }
            //总数
            $count = MiniSubscribeMessageModel::where($where)->count();
            return json(self::createList($list,$count));
        }else if($request->param('_action') == 'delete'){
            $id = $request->param('id');
            $MiniSubscribeMessageModel = MiniSubscribeMessageModel::where('id',$id)->findOrEmpty();
            if($MiniSubscribeMessageModel->isEmpty()){
                return self::createFail('信息不存在');
            }
            $res = $MiniSubscribeMessageModel->delete();
            if($res !== false){
                return self::createSuccess('删除成功');
            }
            return self::createFail('删除失败');
        }else if($request->param('_action') == 'sync'){
            try {
                $SubscribeMessageService = new SubscribeMessageService('wx294975311d4ef718');
                $res = $SubscribeMessageService->syncTemplates();
                return json($res);
            } catch (\Exception $e) {
                return self::createFail($e->getMessage());
            }
        }
        return View::fetch('subscribeMessage',[
           
        ]);
    }

     /**
     * 订阅消息发送记录
     *
     * @Author tanruizheng
     * @DateTime 2021-08-25
     * @return void
     */
    public function messageRecord(Request $request){
        if($request->param('_action') == 'getList'){
            $page = $request->param('page/d',1);
            $limit = $request->param('limit/d',15);
            $where = [];
            //列表
            $list = MiniSendMessageRecordModel::where($where)->page($page,$limit)->select()->toArray();
            foreach ($list as &$item) {
              
            }
            //总数
            $count = MiniSendMessageRecordModel::where($where)->count();
            return json(self::createList($list,$count));
        }else if($request->param('_action') == 'delete'){
            $id = $request->param('id');
            $MiniSendMessageRecordModel = MiniSendMessageRecordModel::where('id',$id)->findOrEmpty();
            if($MiniSendMessageRecordModel->isEmpty()){
                return self::createFail('信息不存在');
            }
            $res = $MiniSendMessageRecordModel->delete();
            if($res !== false){
                return self::createSuccess('删除成功');
            }
            return self::createFail('删除失败');
        }
        return View::fetch('messageRecord',[
           
        ]);
    }
}
